package com.imooc.activity7_workflow;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class Part3_ProcessInstance {

    @Autowired
    private RuntimeService runtimeService;

    /**
     * 初始化流程
     */
    @Test
    public void initProcessInstance(){
        // 1、获取表单填报的内容：请假时间、请假事由，String fromData
        // 2、写入业务表，返回业务表主键 ID
        // 3、把业务数据与Activiti7流程数据关联
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("HouXuanRen_claim", "businessKey004");
        System.out.println("流程实例ID：" + processInstance.getProcessDefinitionId());
    }

    /**
     * 获取流程实例
     */
    @Test
    public void getProcessINstances(){
        List<ProcessInstance> list = runtimeService.createProcessInstanceQuery()
                .list();
        list.forEach(processInstance -> {
            System.out.println("--------流程实例--------");
            System.out.println("ProcessInstanceId：" + processInstance.getProcessInstanceId());
            System.out.println("ProcessDefinitionId：" + processInstance.getProcessDefinitionId());
            System.out.println("isEnded：" + processInstance.isEnded());
            System.out.println("isSuspended：" + processInstance.isSuspended());
        });
    }

    /**
     * 暂停与激活流程实例
     */
    @Test
    public void activeProcessInstance(){
//        runtimeService.suspendProcessInstanceById("1394b85a-4207-11eb-9b56-c23049e74a6b");
//        System.out.println("挂起流程实例");

        runtimeService.activateProcessInstanceById("1394b85a-4207-11eb-9b56-c23049e74a6b");
        System.out.println("激活流程实例");
    }

    /**
     * 删除流程实例
     */
    @Test
    public void deleteProcessInstance(){
        runtimeService.deleteProcessInstance("79c0e825-4785-11eb-b614-c23049e74a6b", "删着玩");
        System.out.println("删除流程实例成功");
    }
}
